<template lang="pug">
  .CreateWagesModal
    Modal(
      v-model="isShowCreateTreatmentParamTypeModal"
      :title="isCreate ? '新增待遇类型' : '修改待遇类型'"
      class-name="vertical-center-modal"
      width="480"
    )
      Form.ivu-row(ref="formCreate" :model="formCreate" :rules="rules" :label-width="100")
        FormItem.form-item.ivu-col(v-for="(item, index) of formItem" :label="item.label" :key="index" :class="item.style" :prop="item.prop")
          template(v-if="item.type === 'input'")
            Input(v-model="formCreate[item.model]" :placeholder="item.placeholder" clearable :disabled="item.isDisabled")
          template(v-if="item.type === 'select'")
            Select(v-model="formCreate[item.model]" :placeholder="item.placeholder" clearable :disabled="item.isDisabled" @on-change="listenCall(item.fn, item.model, $event)")
              Option(v-for="(c_item, c_index) of arrListDown[item.options]" :key="c_index" :value="c_item.value") {{c_item.text}}
          template(v-if="item.type === 'switch'" :disabled="item.isDisabled")
            i-switch(v-model="formCreate[item.model]" :disabled="item.isDisabled")
          template(v-if="item.type === 'radio'")
            RadioGroup.top-3-fu.relative(v-model="formCreate[item.model]")
              Radio.display-inline-block(v-for="(c_item, index) of arrListDown[item.options]" :key="index" :label="c_item.value") {{c_item.label}}
      .clearfix(slot="footer")
        Button(type="text" @click="cancel") 取消
        Button(type="primary" @click="handleSave()" :loading="!isCanSubmit") 保存
</template>

<script>
import { Common } from '@/utils/common.js'
import { TreatmentApi } from '@/api/Treatment.api.js'
import { TreatmentBasicsTypeApi } from '@/api/TreatmentBasicsType.api.js'

export default {
  name: 'CreateTreatmentParamTypeModal',

  // props: ['arrListDown'],

  watch: {
  },

  computed: {
  },

  data () {
    return {
      id: null,
      isShowCreateTreatmentParamTypeModal: false,
      isCreate: true,
      formCreate: {
        parameterType: '',
        treatmentName: '',
        whetherDifferentiateCities: false,
        status: true
      },
      formItem: [
        { type: 'select', label: '参保险种', placeholder: '请选择参保险种', prop: 'parameterType', model: 'parameterType', style: 'ivu-col-span-24', options: 'parametertype',isDisabled:false,fn:'parametertypeChange' },
        { type: 'input', label: '待遇名称', placeholder: '请输入待遇名称', prop: 'treatmentName', model: 'treatmentName', style: 'ivu-col-span-24',isDisabled:false },
        { type: 'switch', label: '区分城市', placeholder: '', prop: 'whetherDifferentiateCities', model: 'whetherDifferentiateCities', style: 'ivu-col-span-24', isDisabled:false  },
        { type: 'switch', label: '启用状态', placeholder: '', prop: 'status', model: 'status', style: 'ivu-col-span-24' },
      ],
      rules: {
        parameterType: [
          { required: true, message: '请选择参保险种', trigger: 'change' }
        ],
        treatmentName: [
          { required: true, message: '请输入待遇名称', trigger: 'change' }
        ]
      },
      arrListDown: {
        defaultRadioArr: null,
        parametertype: null,
        status: null
      },
      isCanSubmit: true
    }
  },

  mounted () {
  },

  methods: {
    cancel () {
      this.isShowCreateTreatmentParamTypeModal = false
    },

    show () {
      // this.formCreate.parameterId = id
      this.isShowCreateTreatmentParamTypeModal = true
      console.log(this.formCreate,'show-1')
      this.$refs.formCreate.resetFields()
      console.log(this.formCreate,'show-2')
      this.isCanSubmit = true
    },

    // 监听所有调用的方法
    listenCall (methodsWords, ...values) {
      this[methodsWords](...values)
    },

    parametertypeChange (model, val) {
      if (val == 'FE566BF6-2CBB-42B6-8868-3363D6465DCB') {
        this.formItem[2].isDisabled = true
        this.formCreate.whetherDifferentiateCities = false
      } else {
        this.formItem[2].isDisabled = false
      }
    },

    // 根据ID获取待遇参数
    async getModel (id) {
      let res = await TreatmentBasicsTypeApi.getModel({id: id})
      console.log('根据ID获取待遇参数: ', res)
      if (res.status === 200 && res.data.success) {
        var data = res.data.data
        console.log(data,'data')
        // Object.keys(this.formCreate).forEach(key => {
        //   this.formCreate[key] = data[key] + '' && data[key] + '' !== 'null' ? data[key] + '' : ''
        // })
        this.formCreate = {
          parameterType: data.insuredTypeId.toUpperCase(),
          status: data.status == 0,
          treatmentName: data.treatmentName,
          whetherDifferentiateCities: data.whetherDifferentiateCities == 0 ? false : true
        }
        this.parametertypeChange('parameterType', data.insuredTypeId.toUpperCase() )
        console.log(this.formCreate)
      } else {
      }
    },

    // 保存
    async handleSave () {
      this.isCanSubmit = false
      console.log('this.formCreate', this.formCreate)
      if(this.formCreate.treatmentName.replace(/\ +/g,"") == ''){
        this.formCreate.treatmentName = ''
        this.isCanSubmit = true
        return
      }
      this.formCreate.treatmentName = this.formCreate.treatmentName.replace(/\ +/g,"")
      this.$refs.formCreate.validate((valid) => {
        if (valid) {
          var params = {
            Id: this.id.toUpperCase(),
            InsuredTypeId: this.formCreate.parameterType.toUpperCase(),
            TreatmentName: this.formCreate.treatmentName.replace(/\ +/g,""),
            WhetherDifferentiateCities: this.formCreate.whetherDifferentiateCities ? 1 : 0,
            Status: this.formCreate.status? 0 : 1
          }
          TreatmentBasicsTypeApi.saveBasicsType(params).then(res => {
            console.log('保存待遇参数: ', res)
            if (res.status === 200 && res.data.success) {
              this.$Message.success(res.data.message)
              this.cancel()
              this.$emit('handleSave')
            } else {
              this.isCanSubmit = true
              this.$Message.error({
                content: res.data.message,
                duration: 10,
                closable: true
              })
            }
          })
        } else {
          this.isCanSubmit = true
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>

</style>
